package com.ruoyi.gis.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gis.domain.DatasetRasterTaskData;
import com.ruoyi.gis.domain.DatasetRasterTaskNode;
import com.ruoyi.gis.flow.StartNodeParam;
import com.ruoyi.gis.service.*;
import com.ruoyi.gis.vo.AddRasterTaskCodeVo;
import com.ruoyi.gis.vo.AddRasterTaskFlowVo;
import com.ruoyi.gis.vo.AddRasterTaskVo;
import com.ruoyi.gis.vo.TaskFlowConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gis.mapper.DatasetRasterTaskMapper;
import com.ruoyi.gis.domain.DatasetRasterTask;

/**
 * 遥感监测任务Service业务层处理
 *
 * @author rww
 * @date 2024-09-20
 */
@Service
public class DatasetRasterTaskServiceImpl implements IDatasetRasterTaskService
{
    @Autowired
    private DatasetRasterTaskMapper datasetRasterTaskMapper;
    @Autowired
    private IDatasetRasterTaskNodeService datasetRasterTaskNodeService;
    @Autowired
    private IDatasetRasterTaskLineService datasetRasterTaskLineService;
    @Autowired
    private IDatasetRasterTaskDataService datasetRasterTaskDataService;
    @Autowired
    private IDatasetRasterCollectedService datasetRasterCollectedService;


    /**
     * 查询遥感监测任务
     *
     * @param id 遥感监测任务主键
     * @return 遥感监测任务
     */
    @Override
    public DatasetRasterTask selectDatasetRasterTaskById(Long id)
    {
        DatasetRasterTask task = datasetRasterTaskMapper.selectDatasetRasterTaskById(id);
        return task;
    }

    /**
     * 查询遥感监测任务列表
     *
     * @param datasetRasterTask 遥感监测任务
     * @return 遥感监测任务
     */
    @Override
    public List<DatasetRasterTask> selectDatasetRasterTaskList(DatasetRasterTask datasetRasterTask)
    {
        return datasetRasterTaskMapper.selectDatasetRasterTaskList(datasetRasterTask);
    }

    /**
     * 新增遥感监测任务
     *
     * @param datasetRasterTask 遥感监测任务
     * @return 结果
     */
    @Override
    public int insertDatasetRasterTask(DatasetRasterTask datasetRasterTask)
    {
        datasetRasterTask.setId(IdUtil.getSnowflakeNextId());
        datasetRasterTask.setCreateTime(DateUtils.getNowDate());
        return datasetRasterTaskMapper.insertDatasetRasterTask(datasetRasterTask);
    }

    /**
     * 修改遥感监测任务
     *
     * @param datasetRasterTask 遥感监测任务
     * @return 结果
     */
    @Override
    public int updateDatasetRasterTask(DatasetRasterTask datasetRasterTask)
    {
        datasetRasterTask.setUpdateTime(DateUtils.getNowDate());
        return datasetRasterTaskMapper.updateDatasetRasterTask(datasetRasterTask);
    }

    /**
     * 批量删除遥感监测任务
     *
     * @param ids 需要删除的遥感监测任务主键
     * @return 结果
     */
    @Override
    public int deleteDatasetRasterTaskByIds(Long[] ids)
    {
        return datasetRasterTaskMapper.deleteDatasetRasterTaskByIds(ids);
    }

    /**
     * 删除遥感监测任务信息
     *
     * @param id 遥感监测任务主键
     * @return 结果
     */
    @Override
    public int deleteDatasetRasterTaskById(Long id)
    {
        return datasetRasterTaskMapper.deleteDatasetRasterTaskById(id);
    }

    @Override
    public int saveTask(LoginUser loginUser,AddRasterTaskVo taskVo) {
        DatasetRasterTask task = taskVo.getTask();
//        TaskConfig config = taskVo.getConfig();
//        task.setFlowName(config.getName());
        task.setStatus(DatasetRasterTask.status_saved);
        if(task.getId()==null){
            task.setUserId(loginUser.getUserId());
            insertDatasetRasterTask(task);
        }else {
            updateDatasetRasterTask(task);
            datasetRasterTaskDataService.cleanTaskDataByTaskId(task.getId());
        }
        taskVo.getDatasets().stream().forEach(data->{
//            DatasetRasterCollected rasterCollected = datasetRasterCollectedService.
//                    selectDatasetRasterCollectedById(data.getCollectId());
//            if(rasterCollected!=null){
//
//            }
            DatasetRasterTaskData taskData = new DatasetRasterTaskData();
            taskData.setDataId(data.getDataId());
            taskData.setRasterId(data.getRasterId());
            taskData.setFileId(data.getFileId());
            taskData.setUserId(loginUser.getUserId());
            taskData.setTaskId(task.getId());
            taskData.setSourceType(data.getSourceType());
            datasetRasterTaskDataService.insertDatasetRasterTaskData(taskData);
        });

        return 1;
    }

    @Override
    public int saveTaskFlow(LoginUser loginUser, AddRasterTaskFlowVo taskVo) {
        DatasetRasterTask task = selectDatasetRasterTaskById(taskVo.getTaskId());
        if(task==null){
            return 0;
        }
        task.setFlowId(taskVo.getTaskId());
        task.setBitmap(taskVo.getBitmap());
        TaskFlowConfig config = taskVo.getConfig();
        datasetRasterTaskNodeService.cleanByTaskId(task.getId());
        config.getNodeList().stream().forEach(node->{
            node.setTaskId(task.getId());
            node.setStatus(10);
            datasetRasterTaskNodeService.insertDatasetRasterTaskNode(node);
            if(DatasetRasterTaskNode.type_start.equals(node.getType())){
                datasetRasterTaskDataService.cleanTaskDataByTaskId(task.getId());
                List<StartNodeParam> startNodeParams = JSON.parseArray(node.getConfig(), StartNodeParam.class);
                if(startNodeParams!=null&&startNodeParams.size()>0){
                    StartNodeParam startNodeParam = startNodeParams.get(0);
                    if(startNodeParam!=null&&startNodeParam.getParams()!=null
                            &&startNodeParam.getParams().getDatasets()!=null)
                    startNodeParam.getParams().getDatasets().stream().forEach(data->{
                        DatasetRasterTaskData taskData = new DatasetRasterTaskData();
                        taskData.setDataId(data.getDataId());
                        taskData.setRasterId(data.getRasterId());
                        taskData.setFileId(data.getFileId());
                        taskData.setUserId(loginUser.getUserId());
                        taskData.setTaskId(task.getId());
                        taskData.setSourceType(data.getSourceType());
                        datasetRasterTaskDataService.insertDatasetRasterTaskData(taskData);
                    });
                }
            }
        });
        datasetRasterTaskLineService.cleanByTaskId(task.getId());
        config.getLineList().stream().forEach(line->{
            line.setTaskId(task.getId());
            datasetRasterTaskLineService.insertDatasetRasterTaskLine(line);
        });
        updateDatasetRasterTask(task);
        return 1;
    }

    @Override
    public int saveTaskCode(LoginUser loginUser, AddRasterTaskCodeVo taskVo) {
        DatasetRasterTask task = selectDatasetRasterTaskById(taskVo.getTaskId());
        if(task==null){
            return 0;
        }
        task.setBitmap(taskVo.getBitmap());
        task.setScriptCode(taskVo.getCode());
        updateDatasetRasterTask(task);
        return 1;
    }
}
